#!/bin/bash

# Single Image Localization Example
# 单张图像定位使用示例

set -e

echo "=== Single Image Localization Example ==="

# # 检查参数
# if [ $# -lt 6 ]; then
#     echo "Usage: $0 <query_image> <sfm_model> <global_features> <local_features> <matches> <output_dir> [num_retrieved] [keep_temp]"
#     echo ""
#     echo "Parameters:"
#     echo "  query_image     - Path to the query image"
#     echo "  sfm_model       - Path to the SfM reconstruction model"
#     echo "  global_features - Path to the global features H5 file"
#     echo "  local_features  - Path to the local features H5 file"
#     echo "  matches         - Path to the matches H5 file"
#     echo "  output_dir      - Output directory"
#     echo "  num_retrieved   - Number of retrieved images (optional, default: 20)"
#     echo "  keep_temp       - Keep temporary files (optional, default: false)"
#     echo ""
#     echo "Example:"
#     echo "  $0 query.jpg sfm_model/ global-feats-netvlad.h5 feats-superpoint.h5 matches-superglue.h5 output/ 20 false"
#     exit 1
# fi

# QUERY_IMAGE="$1"
# SFM_MODEL="$2"
# GLOBAL_FEATURES="$3"
# LOCAL_FEATURES="$4"
# MATCHES="$5"
# OUTPUT_DIR="$6"
# NUM_RETRIEVED="${7:-20}"
# KEEP_TEMP="${8:-false}"


# QUERY_IMAGE="/media/sdisk/data/3dgs/20250731/South-Building/loc/P1180171.JPG"
# SFM_MODEL="/media/sdisk/data/3dgs/20250731/South-Building/outputs/sfm/sfm_superpoint+superglue/"
# GLOBAL_FEATURES="/media/sdisk/data/3dgs/20250731/South-Building/outputs/sfm/global-feats-netvlad.h5"
# LOCAL_FEATURES="/media/sdisk/data/3dgs/20250731/South-Building/outputs/sfm/feats-superpoint-n4096-r1024.h5"
# MATCHES="/media/sdisk/data/3dgs/20250731/South-Building/outputs/sfm/feats-superpoint-n4096-r1024_matches-superglue_pairs-netvlad.h5"
# OUTPUT_DIR="/media/sdisk/data/3dgs/20250731/South-Building/outputs/loc/"
# NUM_RETRIEVED="20"
# KEEP_TEMP="false"
# VISUALIZE="true"
# DB_IMAGE_DIR="/media/sdisk/data/3dgs/20250731/South-Building/images/"


QUERY_IMAGE="/media/sdisk/data/3dgs/220kv/loc/1/DJI_0308.JPG"
SFM_MODEL="/media/sdisk/data/3dgs/220kv/sfm/sfm_superpoint+superglue/"
GLOBAL_FEATURES="/media/sdisk/data/3dgs/220kv/sfm/global-feats-netvlad.h5"
LOCAL_FEATURES="/media/sdisk/data/3dgs/220kv/sfm/feats-superpoint-n4096-r1024.h5"
MATCHES="/media/sdisk/data/3dgs/220kv/sfm/feats-superpoint-n4096-r1024_matches-superglue_pairs-netvlad.h5"
OUTPUT_DIR="/media/sdisk/data/3dgs/220kv/outputs/loc/"
NUM_RETRIEVED="20"
KEEP_TEMP="false"
VISUALIZE="true"
DB_IMAGE_DIR="/media/sdisk/data/3dgs/220kv/images-selected/"

echo "Query image: $QUERY_IMAGE"
echo "SfM model: $SFM_MODEL"
echo "Global features: $GLOBAL_FEATURES"
echo "Local features: $LOCAL_FEATURES"
echo "Matches: $MATCHES"
echo "Output directory: $OUTPUT_DIR"
echo "Number of retrieved images: $NUM_RETRIEVED"
echo "Keep temporary files: $KEEP_TEMP"
echo ""


# 验证输入文件
for file in "$QUERY_IMAGE" "$GLOBAL_FEATURES" "$LOCAL_FEATURES" "$MATCHES"; do
    if [ ! -f "$file" ]; then
        echo "Error: File not found: $file"
        exit 1
    fi
done

if [ ! -d "$SFM_MODEL" ]; then
    echo "Error: SfM model directory not found: $SFM_MODEL"
    exit 1
fi

# 创建输出目录
mkdir -p "$OUTPUT_DIR"

# 运行定位
echo "Starting localization..."
python3 localize_single.py \
    "$QUERY_IMAGE" \
    "$SFM_MODEL" \
    "$GLOBAL_FEATURES" \
    "$LOCAL_FEATURES" \
    "$MATCHES" \
    "$OUTPUT_DIR" \
    "$NUM_RETRIEVED" \
    "$KEEP_TEMP" \
    "$VISUALIZE" \
    "$DB_IMAGE_DIR"

echo ""
echo "Localization completed!"
echo "Results saved in: $OUTPUT_DIR"
